package com.tcs.teg.domain.accounts;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import common.money.Percentage;

@Component
public class AccountRegistrationForm implements Serializable {
	
	private static final Log logger = LogFactory.getLog(AccountRegistrationForm.class);

	private String contactNumber;

	private String accountName;

	private String creditCardNumber;

	private List<Beneficiary> beneficiaries = new ArrayList<Beneficiary>();

	private Boolean formSubmissionStatus = false;

	public String getContactNumber() {
		return contactNumber;
	}

	public void setContactNumber(String contactNumber) {
		logger.debug("setting contact number" + contactNumber);
		this.contactNumber = contactNumber;
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		logger.debug("setting account name" + accountName);
		this.accountName = accountName;
	}

	public String getCreditCardNumber() {
		return creditCardNumber;
	}

	public void setCreditCardNumber(String creditCardNumber) {
		logger.debug("setting account name" + creditCardNumber);
		this.creditCardNumber = creditCardNumber;
	}

	public Boolean getFormSubmissionStatus() {
		return formSubmissionStatus;
	}

	public void setFormSubmissionStatus(Boolean formSubmissionStatus) {
		this.formSubmissionStatus = formSubmissionStatus;
	}

	/**
	 * Returns the beneficiaries for this account.
	 * <p>
	 * Callers should not attempt to hold on or modify the returned set. This
	 * method should only be used transitively; for example, called to
	 * facilitate account reporting.
	 * 
	 * @return the beneficiaries of this account
	 */
	/*
	 * public List<Beneficiary> getBeneficiaries() { Beneficiary bnf = new
	 * Beneficiary(); beneficiaries.add(bnf); return
	 * Collections.unmodifiableList(beneficiaries); //return beneficiaries; }
	 */

	public List<Beneficiary> getBeneficiaries() {
		// beneficiaries.add(new Beneficiary());
		// return Collections.unmodifiableList(beneficiaries);
		return (beneficiaries);
	}

	/**
	 * Add a single beneficiary with a 100% allocation percentage.
	 * 
	 * @param beneficiaryName
	 *            the name of the beneficiary (should be unique)
	 */
	public void addBeneficiary(String beneficiaryName) {
		addBeneficiary(beneficiaryName, Percentage.oneHundred());
	}

	/**
	 * Add a single beneficiary with the specified allocation percentage.
	 * 
	 * @param beneficiaryName
	 *            the name of the beneficiary (should be unique)
	 * @param allocationPercentage
	 *            the beneficiary's allocation percentage within this account
	 */
	public void addBeneficiary(String beneficiaryName,
			Percentage allocationPercentage) {
		beneficiaries
				.add(new Beneficiary(beneficiaryName, allocationPercentage));
	}

	/**
	 * Returns a single account beneficiary. Callers should not attempt to hold
	 * on or modify the returned object. This method should only be used
	 * transitively; for example, called to facilitate reporting or testing.
	 * 
	 * @param name
	 *            the name of the beneficiary e.g "Annabelle"
	 * @return the beneficiary object
	 */
	public Beneficiary getBeneficiary(String name) {
		for (Beneficiary b : beneficiaries) {
			if (b.getName().equals(name)) {
				return b;
			}
		}
		throw new IllegalArgumentException("No such beneficiary with name '"
				+ name + "'");
	}

	/**
	 * Removes a single beneficiary from this account.
	 * 
	 * @param beneficiaryName
	 *            the name of the beneficiary (should be unique)
	 */
	public void removeBeneficiary(String beneficiaryName) {
		beneficiaries.remove(getBeneficiary(beneficiaryName));
	}

	public void setBeneficiaries(ArrayList<Beneficiary> beneficiaries) {
		logger.debug("Setting Beneficiaries ...........................................................");
		logger.debug("Setting Beneficiaries ...........................................................");
		logger.debug("Setting Beneficiaries ...........................................................");
		logger.debug("Setting Beneficiaries ...........................................................");
		this.beneficiaries = beneficiaries;
	}

}
